/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import java.util.ArrayList;

import com.jme3.scene.Node;


/**
 * Right-hand panel of the in-game screen.
 */
public class Panel extends Node {
	
	
	/**
	 * List of all registered click event elements of the panel.
	 */
	private ArrayList<IClickable> clickables = new ArrayList<IClickable>();
	
	
	/**
	 * List of all registered mouseOver event elements of the next turn window.
	 */
	private ArrayList<IMouseOverable> nextTurnWindowMouseOverables = new ArrayList<IMouseOverable>();
	
	
	/**
	 * List of all registered click event elements of the next turn window.
	 */
	private ArrayList<IClickable> nextTurnWindowClickables = new ArrayList<IClickable>();
	
	
	/**
	 * List of all registered mouseOver event elements before the next turn window opened.
	 */
	private ArrayList<IMouseOverable> backedUpMouseOverables = new ArrayList<IMouseOverable>();
	
	
	/**
	 * List of all registered click event elements before the next turn window opened.
	 */
	private ArrayList<IClickable> backedUpClickables = new ArrayList<IClickable>();
	
	
	/**
	 * Geometry node of all content on the panel.
	 */
	private Node panel;
	
	
	/**
	 * Geometry node of the player names and points.
	 */
	private Node playerNames;

	
	/**
	 * Window informing the player of the next turn.
	 */
	private Node nextTurnWindow;
	
	
	/**
	 * Window element of the next turn window.
	 */
	private GuiWindow nextTurnWindowWindow;
	
	
	/**
	 * OK button of the next turn window.
	 */
	private GuiButton nextTurnWindowOk;
	
	
	/**
	 * Lines of text of the turn window, informing the player of a new season or of the last turn.
	 */
	private ArrayList<String> nextTurnTextLines = new ArrayList<String>();
	
	
	/**
	 * Parent node of the lines of text of the turn window, informing the player of a new season or of the last turn.
	 */
	private Node nextTurnTextLinesNode;
	
	
	/**
	 * Saves the player whose turn it current is if the game didn't start yet for this client.
	 * (This happens because there is a delay between when the game starts on the server
	 * and when it is displayed on the clients screen.)
	 */
	private int firstPlayer = -1;
	
	
	/**
	 * Index of the current season
	 */
	private int season;
	
	
	/**
	 * True if a new season has started, but has not been reported yet.
	 */
	private boolean newSeason = false;
	
	
	/**
	 * Season icon displaying the symbol of the current season.
	 */
	private GuiQuad seasonIcon;
	

	/**
	 * Star symbol to the left of the player's prestige points.
	 */
	private GuiQuad pointsStar;
	
	
	/**
	 * Current number of prestige points the player has.
	 */
	private GuiText playerPoints;
	
	
	/**
	 * Icon showing that the player is currently the protege of the caliph.
	 */
	private GuiQuad protegeOfTheCaliph;
	
	
	/**
	 * Coin symbol to the left of the player's money balance
	 */
	private GuiQuad coin;
	
	
	/**
	 * Current money balance of the player.
	 */
	private GuiText playerMoney;
	
	
	/**
	 * All buying agents the player currently has.
	 */
	private ArrayList<GuiBuyingAgent> buyingAgents = new ArrayList<GuiBuyingAgent>();
	
	
	/**
	 * Parent node of all buying agents the player currently has.
	 */
	private Node buyingAgentsNode;
	
	
	/**
	 * Number of players on the server minus 2 to determine the number of buying 
	 * agent slots (9 for 2 players, 8 for 3 players, 7 for 4 players).
	 */
	private int numberOfPlayersMinusTwo = -1;
	
	
	/**
	 * Parent node of all buying agent slots.
	 */
	private Node buyingAgentSlotsNode;
	
	
	/**
	 * Parent node of all tower elements the player currently has in his inventory.
	 */
	private Node towerElementNode;
	
	
	/**
	 * True if in-game screen has already been loaded during this game.
	 */
	private boolean panelHasBeenLoaded = false;
	
	
	/**
	 * X coordinates of the buying agent slots in the panel.
	 */
	private static final int[][] buyingAgentSlotsX = {
		{12, 60, 108, 156, 204, 36, 84, 132, 180},
		{36, 84, 132, 180, 36, 84, 132, 180},
		{36, 84, 132, 180, 60, 108, 156}
	};
	
	
	/**
	 * Y coordinates of the buying agent slots in the panel.
	 */
	private static final int[][] buyingAgentSlotsY = {
		{312, 312, 312, 312, 312, 395, 395, 395, 395},
		{312, 312, 312, 312, 395, 395, 395, 395},
		{312, 312, 312, 312, 395, 395, 395}
	};
	
	
	/**
	 * X coordinates of the names and points of the players.
	 */
	private static final int[][] playerNamesX = {
		{192, 576},
		{192, 384, 576},
		{96, 288, 480, 672}
	};
	
	
	/**
	 * List of the numbers displaying the players' points.
	 */
	private ArrayList<GuiText> playerNamePoints = new ArrayList<GuiText>();
	
	
	/**
	 * Time position of the player names and points display animation.
	 */
	private float timer = 1.0f;
	
	
	/**
	 * Constructor: Creates geometry.
	 */
	public Panel() {
		
		panel = new Node();
		
		GuiQuad background = new GuiQuad();
		background.setPositionSize(GUI.Offset.PANEL, 0, 0, 256, 768, 0);
		background.setMaterial(MaterialManager.getMarble(), 0.0f, 0.0f, 0.66666f, 2.0f);
		panel.attachChild(background.create());
		
		GuiQuad lineLeft = new GuiQuad();
		lineLeft.setPositionSize(GUI.Offset.PANEL, -21, 0, 40, 768, 1);
		lineLeft.setMaterial(MaterialManager.getWindow(), 0.0f, 0.375f, 0.25f, 0.25f);
		panel.attachChild(lineLeft.create());
		
		GuiQuad logo = new GuiQuad();
		logo.setPositionSize(GUI.Offset.PANEL, 0, 0, 256, 75, 2);
		logo.setMaterial(MaterialManager.getPanelLogo(), 0.0f, 0.0f, 1.0f, 1.0f);
		panel.attachChild(logo.create());
		
		seasonIcon = new GuiQuad();
		seasonIcon.setPositionSize(GUI.Offset.PANEL, 0, 95, 256, 120, 3);
		seasonIcon.setMaterial(MaterialManager.getSeasons(), 0.0f, 0.0f, 1.0f, 0.25f);
		panel.attachChild(seasonIcon.create());
		
		pointsStar = new GuiQuad();
		pointsStar.setPositionSize(GUI.Offset.PANEL, 55, 235, 40, 40, 3);
		pointsStar.setMaterial(MaterialManager.getPanelIcons(), 0.0f, 0.0f, 0.5f, 0.5f);
		panel.attachChild(pointsStar.create());
		
		playerPoints = new GuiText("0", Fonts.getLarge());
		playerPoints.setPositionSize(GUI.Offset.PANEL, 100, 242, 3);
		panel.attachChild(playerPoints);
		
		protegeOfTheCaliph = new GuiQuad();
		protegeOfTheCaliph.setPositionSize(GUI.Offset.PANEL, 165, 235, 40, 42, 3);
		protegeOfTheCaliph.setMaterial(MaterialManager.getPanelIcons(), 0.5f, 0.0f, 0.5f, 0.5f);
		panel.attachChild(protegeOfTheCaliph.create());
		
		buyingAgentSlotsNode = new Node();
		panel.attachChild(buyingAgentSlotsNode);
		
		buyingAgentsNode = new Node();
		panel.attachChild(buyingAgentsNode);
		
		playerMoney = new GuiText("0");
		playerMoney.setPositionSize(GUI.Offset.PANEL, (128 + 40 - (((int)playerMoney.getLineWidth() + 50) / 2)), 489, 3);
		panel.attachChild(playerMoney);
		
		coin = new GuiQuad();
		coin.setPositionSize(GUI.Offset.PANEL, (128 - (((int)playerMoney.getLineWidth() + 50) / 2)), 479, 40, 40, 3);
		coin.setMaterial(MaterialManager.getPanelIcons(), 0.0f, 0.5f, 0.5f, 0.5f);
		panel.attachChild(coin.create());
		
		for(int i = 0; i < 16; i++) {
			GuiSlot slot = new GuiSlot(GUI.Offset.PANEL, 13 + 60 * (i % 4), 528 + 60 * (i / 4), 50, 50, 3);
			panel.attachChild(slot);
		}
		
		towerElementNode = new Node();
		panel.attachChild(towerElementNode);
		
		
		// Scenery
		GuiInteractiveArea clickables3D = new GuiInteractiveArea(GUI.Offset.SCENE, 0, 0, 768, 768);
		clickables3D.setAction(new ICallback() {
			public void run() {
				Scenery.processMouseClick();
			}
		});
		clickables.add(clickables3D);
		
		
		// Next turn window
		
		nextTurnWindow = new Node();
		
		nextTurnWindowWindow = new GuiWindow(GUI.Offset.SCENE, (768 - 600) / 2, 275, 600, 215, 100);
		nextTurnWindow.attachChild(nextTurnWindowWindow);
		
		nextTurnTextLinesNode = new Node();
		nextTurnWindow.attachChild(nextTurnTextLinesNode);
        
		nextTurnWindowOk = new GuiButton("OK");
		nextTurnWindowOk.setPositionSize(GUI.Offset.SCENE, (768 - 200) / 2, 405, 200, 101);
		nextTurnWindowOk.setAction(new ICallback() { 
			public void run() {
				nextTurnTextLines.clear();
				GUI.unregisterClickable(nextTurnWindowClickables);
				GUI.unregisterMouseOverable(nextTurnWindowMouseOverables);
				panel.detachChild(nextTurnWindow);
				GUI.registerClickable(backedUpClickables);
				GUI.registerMouseOverable(backedUpMouseOverables);
			}
		});
		nextTurnWindow.attachChild(nextTurnWindowOk.create());
		
		nextTurnWindowClickables.add(nextTurnWindowOk);
		nextTurnWindowMouseOverables.add(nextTurnWindowOk);	
		
		playerNames = new Node();
		
	}
	
	
	/**
	 * Start game: attach panel and set buying agent slots.
	 */
	public void startGame() {
		if(!panelHasBeenLoaded) {
			
			Scenery.createTerrain(Game.board().getPlayerNum());
			showPlayerNames();
			
			if(numberOfPlayersMinusTwo == -1) {
				numberOfPlayersMinusTwo = Game.board().getPlayerNum() - 2;
			}
			
			this.attachChild(panel);
			
			for(int i = 0; i < buyingAgentSlotsX[numberOfPlayersMinusTwo].length; i++) {
				GuiSlot slot = new GuiSlot(GUI.Offset.PANEL, buyingAgentSlotsX[numberOfPlayersMinusTwo][i], buyingAgentSlotsY[numberOfPlayersMinusTwo][i], 40, 75, 3);
				buyingAgentSlotsNode.attachChild(slot);
			}
			panelHasBeenLoaded = true;
			
			nextPlayerTurn(Game.board().getTurn(), -1, -1, -1);
		}
	}
	
	
	/**
	 * Sets up player names and points display in the scene.
	 */
	public void showPlayerNames() {
		int numberOfPlayers = Game.board().getPlayerNum();
		for(int i = 0; i < numberOfPlayers; i++) {
			
			GuiText playerName = new GuiText(Game.board().getPlayer(i).getName(), Fonts.getOutline());
			playerName.setPositionSize(GUI.Offset.SCENE, playerNamesX[numberOfPlayers - 2][i] - ((int) (playerName.getLineWidth() - 35) / 2), 718, 100);
			playerNames.attachChild(playerName);
			
			GuiQuad playerColor = new GuiQuad();
			playerColor.setPositionSize(GUI.Offset.SCENE, playerNamesX[numberOfPlayers - 2][i] - ((int) (playerName.getLineWidth() + 45) / 2), 708, 40, 40, 100);
			playerColor.setMaterial(MaterialManager.getPlayerColors(), 0.08333f * Game.board().getPlayer(i).getColor(), 0.0f, 0.08333f, 1.0f);
			playerNames.attachChild(playerColor.create());
			
			GuiText playerPoints = new GuiText("0", Fonts.getOutline());
			playerPoints.setPositionSize(GUI.Offset.SCENE, playerNamesX[numberOfPlayers - 2][i] - ((int) (playerPoints.getLineWidth() - 35) / 2), 743, 100);
			playerNames.attachChild(playerPoints);
			
			GuiQuad playerPointsStar = new GuiQuad();
			playerPointsStar.setPositionSize(GUI.Offset.SCENE, playerNamesX[numberOfPlayers - 2][i] - ((int) (playerPoints.getLineWidth() + 45) / 2), 733, 40, 40, 100);
			playerPointsStar.setMaterial(MaterialManager.getPanelIcons(), 0.5f, 0.5f, 0.5f, 0.5f);
			playerNames.attachChild(playerPointsStar.create());
			
			playerNamePoints.add(playerPoints);
		}
	}
	
	
	/**
	 * Attach all needed geometry and register all mouseOver and click events.
	 */
	public void show() {
		GUI.registerClickable(clickables);
		panel.attachChild(playerNames);
		timer = 0.0f;
	}
	
	
	/**
	 * Detach all geometry and unregister all mouseOver and click events.
	 */
	public void hide() {
		GUI.unregisterClickable(clickables);
		panel.detachChild(playerNames);
	}


	/**
	 * Updates money balance, list of buying agents and inventory.
	 */
	public void updateMoneyBuyingAgentsInventory() {
		
		if(numberOfPlayersMinusTwo == -1) {
			numberOfPlayersMinusTwo = Game.board().getPlayerNum() - 2;
		}
		
		int newPlayerPoints = Game.board().me().getPoints();
		playerPoints.setText(Integer.toString(newPlayerPoints));
		
		boolean playerIsProtege = Game.board().getMyIndex() == Game.board().getProtege();
		
		if(playerIsProtege && !panel.hasChild(protegeOfTheCaliph)) {
			panel.attachChild(protegeOfTheCaliph);
			
			playerPoints.setPositionSize(GUI.Offset.PANEL, 100, 242, 3);
			pointsStar.moveTo(GUI.Offset.PANEL, 55, 235, 3);
			
		} else if(!playerIsProtege && panel.hasChild(protegeOfTheCaliph)) {
			panel.detachChild(protegeOfTheCaliph);
		}
		
		if(!playerIsProtege) {
			playerPoints.setPositionSize(GUI.Offset.PANEL, (128 + 45 - (((int)playerPoints.getLineWidth() + 55) / 2)), 242, 3);
			pointsStar.moveTo(GUI.Offset.PANEL, (128 - (((int)playerPoints.getLineWidth() + 55) / 2)), 235, 3);
		}
		
		buyingAgentsNode.detachAllChildren();
		ArrayList<BuyingAgent> newBuyingAgents = Game.board().me().getBuyingAgents();
		
		if(buyingAgents.size() == 0) {
			int index = 0;
			for(BuyingAgent newBuyingAgent : newBuyingAgents) {
				GuiBuyingAgent newGuiBuyingAgent = new GuiBuyingAgent(newBuyingAgent.getId());
				buyingAgents.add(newGuiBuyingAgent);
				buyingAgentsNode.attachChild(newGuiBuyingAgent.get2D(GUI.Offset.PANEL, buyingAgentSlotsX[numberOfPlayersMinusTwo][index], buyingAgentSlotsY[numberOfPlayersMinusTwo][index], 4));
				index++;
			}
		} else {
			int index = 0;
			for(GuiBuyingAgent buyingAgent : buyingAgents) {
				boolean used = true;
				for(BuyingAgent newBuyingAgent : newBuyingAgents) {
					if(newBuyingAgent.getId() == buyingAgent.getId()) {
						buyingAgentsNode.attachChild(buyingAgent.get2D(GUI.Offset.PANEL, buyingAgentSlotsX[numberOfPlayersMinusTwo][index], buyingAgentSlotsY[numberOfPlayersMinusTwo][index], 4));
						used = false;
					}
				}
				if(used) {
					buyingAgent.setUsed(true);
				}
				index++;
			}
		}
		
		int newPlayerMoney = Game.board().me().getMoneyBalance();
		playerMoney.setText(Integer.toString(newPlayerMoney));
		playerMoney.setPositionSize(GUI.Offset.PANEL, (128 + 40 - (((int)playerMoney.getLineWidth() + 50) / 2)), 489, 3);
		coin.moveTo(GUI.Offset.PANEL, (128 - (((int)playerMoney.getLineWidth() + 50) / 2)), 479, 3);
		
		towerElementNode.detachAllChildren();
		ArrayList<TowerElement> towerElements = Game.board().me().getInventory();
		
		int index = 0;
		for(TowerElement towerElement : towerElements) {
			GuiTowerElement guiTowerElement = new GuiTowerElement(towerElement);
			towerElementNode.attachChild(guiTowerElement.get2D(GUI.Offset.PANEL, 38 + 60 * (index % 4), 568 + 60 * (index / 4), 4, 1.0f));
			index++;
		}
		
		for(int i = 0; i < playerNamePoints.size(); i++) {
			playerNamePoints.get(i).setText(Integer.toString(Game.board().getPlayer(i).getPoints()));
		}
		
	}
	
	
	/**
	 * Deletes all buying agents und updates panel.
	 */
	public void newSeason() {
		
		season = Game.board().getSeason();
		if(season > 4) {
			season = 4;
		}
		seasonIcon.changeTextureCoords(0.0f, (season - 1) * 0.25f, 1.0f, 0.25f);
		buyingAgentsNode.detachAllChildren();
		buyingAgents.clear();
		updateMoneyBuyingAgentsInventory();
		newSeason = true;
	}


	/**
	 * Get a list of all buying agents on the panel.
	 */
	public ArrayList<GuiBuyingAgent> getBuyingAgents() {
		return buyingAgents;
	}


	/**
	 * Displays a window informing the player of the next turn.
	 */
	public void nextPlayerTurn(int nextPlayerIndex, int lastPlayerIndex, int lastPlayerActionLocation, int lastPlayerActionDetail) {
		if(!panelHasBeenLoaded) {
			firstPlayer = nextPlayerIndex;
			return;
		}
		if(nextTurnTextLines.size() > 0) {
			nextTurnTextLines.remove(nextTurnTextLines.size() - 1);
		}
		
		if(lastPlayerIndex != -1) {
			if(lastPlayerIndex != Game.board().getMyIndex()) {
				String lastPlayer = Game.board().getPlayer(lastPlayerIndex).getName();
				if(lastPlayerActionLocation == 1) {
					if(lastPlayerActionDetail == 0) {
						nextTurnTextLines.add(lastPlayer + " hat 12 Goldm\u00FCnzen in der Bank bezogen.");
					} else if(lastPlayerActionDetail == 1) {
						nextTurnTextLines.add(lastPlayer + " hat 10 Goldm\u00FCnzen in der Bank bezogen.");
					} else if(lastPlayerActionDetail == 2) {
						nextTurnTextLines.add(lastPlayer + " hat 8 Goldm\u00FCnzen in der Bank bezogen.");
					} else if(lastPlayerActionDetail == 3) {
						nextTurnTextLines.add(lastPlayer + " hat 5 Goldm\u00FCnzen in der Bank bezogen.");
					}
				} else if(lastPlayerActionLocation == 2) {
					if(lastPlayerActionDetail == 1) {
						nextTurnTextLines.add(lastPlayer + " hat 1 Turmteil gebaut.");
					} else {
						nextTurnTextLines.add(lastPlayer + " hat " + lastPlayerActionDetail + " Turmteile gebaut.");
					}

				} else if(lastPlayerActionLocation == 3) {
					if(lastPlayerActionDetail == 1) {
						nextTurnTextLines.add(lastPlayer + " hat ein Basisteil im Turmteil-Markt gekauft.");
					} else if(lastPlayerActionDetail == 2) {
						nextTurnTextLines.add(lastPlayer + " hat ein Mittelteil im Turmteil-Markt gekauft.");
					} else if(lastPlayerActionDetail == 3) {
						nextTurnTextLines.add(lastPlayer + " hat ein Fenster im Turmteil-Markt gekauft.");
					} else if(lastPlayerActionDetail == 4) {
						nextTurnTextLines.add(lastPlayer + " hat eine Spitze im Turmteil-Markt gekauft.");
					}
				} else if(lastPlayerActionLocation == 4) {
					if(lastPlayerActionDetail == 1) {
						nextTurnTextLines.add(lastPlayer + " hat den Basisteil-Markt bestochen.");
					} else if(lastPlayerActionDetail == 2) {
						nextTurnTextLines.add(lastPlayer + " hat den Mittelteil-Markt bestochen.");
					} else if(lastPlayerActionDetail == 3) {
						nextTurnTextLines.add(lastPlayer + " hat den Turmteil-Markt f\u00FCr Fenster bestochen.");
					} else if(lastPlayerActionDetail == 4) {
						nextTurnTextLines.add(lastPlayer + " hat den Turmteil-Markt f\u00FCr Spitzen bestochen.");
					}
					
				} else if(lastPlayerActionLocation == 5) {
					nextTurnTextLines.add(lastPlayer + " ist jetzt der Prot\u00E9g\u00E9 des Kalifen.");
				} else if(lastPlayerActionLocation == 6) {
					nextTurnTextLines.add(lastPlayer + " hat gecheatet!!!");
				}
			}
		}
		
		if(newSeason) {
			if(season == 1) {
				nextTurnTextLines.add("Willkommen im neuen Spiel. Es ist Winter.");
			} else if(season == 2) {
				nextTurnTextLines.add("Der Fr\u00FChling hat begonnen!");
			} else if(season == 3) {
				nextTurnTextLines.add("Der Sommer hat begonnen!");
			} else if(season == 4) {
				nextTurnTextLines.add("Der Herbst hat begonnen!");
			}
			newSeason = false;
		}
		
		if(firstPlayer != -1) {
			nextPlayerIndex = firstPlayer;
			firstPlayer = -1;
		}
		
		if(nextPlayerIndex != -1) {
			if(nextPlayerIndex == Game.board().getMyIndex()) {
				nextTurnTextLines.add("Du bist jetzt am Zug.");
			} else {
				nextTurnTextLines.add(Game.board().getPlayer(nextPlayerIndex).getName() + " ist jetzt am Zug.");
			}
		}
		
		nextTurnTextLinesNode.detachAllChildren();
		
		int windowHeight = 145 + (30 * nextTurnTextLines.size());
		int lineOffset = 370 + 40 - (windowHeight / 2);
		for(String line : nextTurnTextLines) {
			GuiText guiLine = new GuiText(line);
			guiLine.setPositionSize(GUI.Offset.SCENE, (768 - (int)guiLine.getLineWidth()) / 2, lineOffset, 101);
			nextTurnTextLinesNode.attachChild(guiLine);
			lineOffset += 30;
		}

		if(!panel.hasChild(nextTurnWindow)) {
			backedUpClickables.clear();
			backedUpMouseOverables.clear();
			backedUpClickables.addAll(GUI.getClickables());
			backedUpMouseOverables.addAll(GUI.getMouseOverables());
			GUI.unregisterClickable(backedUpClickables);
			GUI.unregisterMouseOverable(backedUpMouseOverables);
			
			panel.attachChild(nextTurnWindow);
			GUI.registerClickable(nextTurnWindowClickables);
			GUI.registerMouseOverable(nextTurnWindowMouseOverables);
		}
		
		nextTurnWindow.detachChild(nextTurnWindowWindow);
		nextTurnWindowWindow = new GuiWindow(GUI.Offset.SCENE, (768 - 600) / 2, 370 - (windowHeight / 2), 600, windowHeight, 100);
		nextTurnWindow.attachChild(nextTurnWindowWindow);
		
		nextTurnWindowOk.move(GUI.Offset.SCENE, (768 - 200) / 2, lineOffset + 20, 101);

	}
	
	
	public void update(float delta) {
		if(timer < 0.6f) {
			timer += delta;
			if(timer >= 0.6f) {
				playerNames.setLocalTranslation(0.0f, 0.0f, 0.0f);
			} else {
				playerNames.setLocalTranslation(0.0f, -60.0f * (float) Math.sin((0.6f - timer) * (Math.PI * 0.8333)), 0.0f);
			}
		}
	}

	/**
	 * Display end of game window.
	 */
	public void displayEndOfGameWindow() {
		Node endOfGameWindow = new Node();
		
		GuiWindow endOfGameWindowWindow = new GuiWindow(GUI.Offset.SCENE, (768 - 600) / 2, 245, 600, 250, 100);
		endOfGameWindow.attachChild(endOfGameWindowWindow);
		
		int rank = 1;
		int myScore = Game.board().me().getPoints();
		for(int i = 0; i < Game.board().getPlayerNum(); i++) {
			if(i != Game.board().getMyIndex()) {
				if(Game.board().getPlayer(i).getPoints() > myScore) {
					rank++;
				}
			}
		}
		String line = "";
		if(rank == 1) {
			line = "Du hast gewonnen!";
		} else {
			line = "Du bist auf dem " + rank + ". Platz!";
		}
		
		GuiText guiLine = new GuiText(line, Fonts.getLarge());
		guiLine.setPositionSize(GUI.Offset.SCENE, (768 - (int)guiLine.getLineWidth()) / 2, 315, 101);
		endOfGameWindow.attachChild(guiLine);
		
		GuiButton quitGame = new GuiButton("Spiel beenden");
		quitGame.setPositionSize(GUI.Offset.SCENE, (768 - 200) / 2, 410, 200, 101);
		quitGame.setAction(new ICallback() { 
			public void run() {
				GUI.quit();
			}
		});
		endOfGameWindow.attachChild(quitGame.create());
		
		ArrayList<IClickable> deadClickables = new ArrayList<IClickable>();
		deadClickables.addAll(GUI.getClickables());
		GUI.unregisterClickable(deadClickables);
		
		ArrayList<IMouseOverable> deadMouseOverables = new ArrayList<IMouseOverable>();
		deadMouseOverables.addAll(GUI.getMouseOverables());
		GUI.unregisterMouseOverable(deadMouseOverables);
		
		ArrayList<IClickable> endOfGameClickables = new ArrayList<IClickable>();
		endOfGameClickables.add(quitGame);
		GUI.registerClickable(endOfGameClickables);
		
		ArrayList<IMouseOverable> endOfGameMouseOverables = new ArrayList<IMouseOverable>();
		endOfGameMouseOverables.add(quitGame);
		GUI.registerMouseOverable(endOfGameMouseOverables);
		
		GUI.getNode().attachChild(endOfGameWindow);
	}
	
}
